<template>
  <div class="home-category" @mouseleave="goodsIndex = ''">
    <ul class="menu">
      <li
        v-for="item in category.cateList"
        :key="item.id"
        @mouseenter="getItemIndex(item.id)"
        :class="{ active: item.id === goodsIndex }"
      >
        <!-- 大文字 -->
        <RouterLink to="/">{{ item.name }}</RouterLink>
        <!-- 小文字: 在循环中原本数据有4个 经过  slice 处理之后只循环2个 -->

        <template v-if="item.children">
          <RouterLink to="/" v-for="subItem in item.children?.slice(0, 2)" :key="subItem.id">
            {{ subItem.name }}
          </RouterLink>
        </template>

        <gujiaping
          v-else
          v-for="item in 2"
          :key="item"
          :animated="true"
          :width="70"
          :height="20"
          :bg="'#eee'"
          :fade="true"
          style="margin-right: 10px"
        ></gujiaping>
      </li>
    </ul>
    <!-- 弹出层 -->
    <div class="layer" v-show="showList">
      <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <ul>
        <li v-for="item in showList" :key="item.id">
          <router-link to="/">
            <img :src="item.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ item.desc }}</p>
              <p class="desc ellipsis">{{ item.name }}</p>
              <p class="price"><i>¥</i>{{ item.price }}</p>
            </div>
          </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>
<script lang="ts" setup name="HomeCategory">
import useStore from '@/store'

import { computed, ref } from 'vue'
const { category } = useStore()
const goodsIndex = ref('')
const getItemIndex = (id: string) => {
  console.log(`拿到每一个li的ID`, id)
  if (!id) return
  return (goodsIndex.value = id)
}
// 计算属性 : 鼠标滑入那个li的id做出判断并找到这个抛出
const showList = computed(() => {
  return category.cateList.find((item) => item.id === goodsIndex.value)?.goods
})
</script>

<style scoped lang="less">
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  z-index: 99;
  position: relative; // 和 z-index 一起
  .menu {
    li {
      padding-left: 40px;
      height: 55px;
      line-height: 55px;
      // 谁激活谁加 active
      &:hover,
      &.active {
        background: @xtxColor;
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 18px;
        }
      }
    }
  }
  .layer {
    background: rgba(255, 255, 255, 0.8);
    width: 990px;
    height: 500px;
    position: absolute;
    left: 250px;
    top: 0;
    // display: none;   //隐藏
    padding: 0 15px;
    h4 {
      line-height: 80px;
      font-weight: normal;
      font-size: 20px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          //3n 表示每行的第3个
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            min-width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
            }
            .desc {
              font-size: 12px;
              color: #888;
            }
            .price {
              font-size: 20px;
              color: @priceColor;
              i {
                font-size: 14px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
